package com.wang.project.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.project.common.ErrorCode;
import com.wang.project.constant.UserConstant;
import com.wang.project.exception.BusinessException;
import com.wang.project.model.dto.integral.IntegralOrderQueryRequest;
import com.wang.project.model.dto.integral.IntegralOrderUpdateRequest;
import com.wang.project.model.entity.IntegralOrder;
import com.wang.project.model.entity.User;
import com.wang.project.model.vo.integral.IntegralOrderVO;
import com.wang.project.service.IntegralOrderService;
import com.wang.project.mapper.IntegralOrderMapper;
import com.wang.project.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 胡歌的腿毛
 * @description 针对表【integral_order】的数据库操作Service实现
 * @createDate 2023-09-27 16:07:36
 */
@Service
@Slf4j
public class IntegralOrderServiceImpl extends ServiceImpl<IntegralOrderMapper, IntegralOrder> implements IntegralOrderService {

    @Resource
    private IntegralOrderMapper integralOrderMapper;

    @Resource
    private UserService userService;

    /**
     * 查询积分订单数据
     *
     * @param integralOrderQueryRequest
     * @param request
     * @return
     */
    @Override
    public List<IntegralOrderVO> listIntegralOrder(IntegralOrderQueryRequest integralOrderQueryRequest, HttpServletRequest request) {
        log.info("IntegralOrderServiceImpl listIntegralOrder integralOrderQueryRequest:{}", JSONUtil.toJsonStr(integralOrderQueryRequest));
        User loginUser = userService.getLoginUser(request);
        if (loginUser.getUserRole().equals(UserConstant.ADMIN_ROLE)) {
            //管理员身份，获取所有用户的订单消息
            List<IntegralOrderVO> integralOrderVOS = integralOrderMapper.listIntegralOrder(integralOrderQueryRequest);
            log.info("IntegralOrderServiceImpl listIntegralOrder integralOrderVOS:{}", JSONUtil.toJsonStr(integralOrderVOS));
            return integralOrderVOS;
        } else {
            //用户身份，只返回用户自己的订单消息
            QueryWrapper<IntegralOrder> integralOrderQueryWrapper = new QueryWrapper<>();
            integralOrderQueryWrapper.eq("userId", loginUser.getId());
            List<IntegralOrder> integralOrders = this.list(integralOrderQueryWrapper);
            List<IntegralOrderVO> integralOrderVOS = integralOrders.stream().map(item -> {
                IntegralOrderVO integralOrderVO = new IntegralOrderVO();
                integralOrderVO.setOrderCode(item.getOrderCode());
                integralOrderVO.setExpendIntegralAmount(item.getExpendIntegralAmount());
                integralOrderVO.setCreateTime(item.getCreateTime());
                integralOrderVO.setExchangeType(item.getExchangeType());
                integralOrderVO.setUserName(loginUser.getUserName());
                return integralOrderVO;
            }).collect(Collectors.toList());
            return integralOrderVOS;
        }
    }


    /**
     * 分页查询积分订单
     *
     * @param integralOrderQueryRequest
     * @param request
     * @return
     */
    @Override
    public Page<IntegralOrderVO> pageIntegralOrder(IntegralOrderQueryRequest integralOrderQueryRequest, HttpServletRequest request) {
        log.info("IntegralOrderServiceImpl pageIntegralOrder integralOrderQueryRequest:{}", JSONUtil.toJsonStr(integralOrderQueryRequest));
        long size = 10;
        long current = 1;
        String userName = integralOrderQueryRequest.getUserName(); //单独查询条件
        IntegralOrder integralOrder = new IntegralOrder();
        if (integralOrderQueryRequest != null) {
            BeanUtils.copyProperties(integralOrderQueryRequest, integralOrder);
            current = integralOrderQueryRequest.getCurrent();
            size = integralOrderQueryRequest.getPageSize();
        }
        QueryWrapper<IntegralOrder> queryWrapper = new QueryWrapper<>(integralOrder);
        //返回的page数据 order中的数据
        Page<IntegralOrder> integralOrderPage = this.page(new Page<>(current, size), queryWrapper);
        Page<IntegralOrderVO> integralOrderVOPage = new PageDTO<>(integralOrderPage.getCurrent(), integralOrderPage.getSize(), integralOrderPage.getTotal());//page类

        //判断userName查询条件
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq(Objects.nonNull(userName), "userName", userName);
        List<User> userList = userService.list(userQueryWrapper);

        //组装数据 订单数据量大的情况可能会很耗时
        List<IntegralOrderVO> integralOrderVOS = integralOrderPage.getRecords().stream().map(item -> {
            IntegralOrderVO integralOrderVO = new IntegralOrderVO();
            BeanUtils.copyProperties(item, integralOrderVO);
            Long userId = item.getUserId();
            List<User> collect = userList.stream().filter(user -> user.getId().equals(userId)).collect(Collectors.toList());
            integralOrderVO.setUserName(collect.get(0).getUserName());
            return integralOrderVO;
        }).collect(Collectors.toList());

        //返回
        integralOrderVOPage.setRecords(integralOrderVOS);
        return integralOrderVOPage;

    }

    /**
     * 更新订单信息
     *
     * @param integralOrderUpdateRequest
     * @return
     */
    @Override
    public Boolean updateIntegralOrder(IntegralOrderUpdateRequest integralOrderUpdateRequest) {
        if (integralOrderUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        log.info("IntegralOrderServiceImpl updateIntegralOrder integralOrderUpdateRequest:{}", JSONUtil.toJsonStr(integralOrderUpdateRequest));
        IntegralOrder integralOrder = new IntegralOrder();
        BeanUtils.copyProperties(integralOrderUpdateRequest, integralOrder);
        QueryWrapper<IntegralOrder> integralOrderQueryWrapper = new QueryWrapper<>(integralOrder);
        return this.update(integralOrderQueryWrapper);
    }
}




